package com.lucy.rabc.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.rabc.entity.SysPost;
import com.lucy.rabc.entity.SysUser;
import com.lucy.rabc.params.PostCreateParams;
import com.lucy.rabc.params.PostQueryParams;
import com.lucy.rabc.params.PostUpdateParams;
import com.lucy.rabc.service.SysPostService;
import com.lucy.rabc.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Response;
import java.util.List;
import java.util.Optional;

/**
 * @author kindear
 */
@Slf4j
@RestController
@RequestMapping("system")
@Api(tags = "岗位管理")
public class PostController {
    @Autowired
    SysPostService postService;

    @Autowired
    SysUserService userService;

    @PostMapping("posts")
    @ApiOperation(value = "岗位新建",notes = "")
    public ResponseDTO<Object> postCreate(@RequestBody @Validated PostCreateParams params){
        SysPost post = new SysPost();
        BeanUtil.copyProperties(params,post, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(postService.saveOne(post));
    }

    @DeleteMapping("posts/{postId}")
    @ApiOperation(value = "岗位删除",notes = "")
    public ResponseDTO<Object> postDelete(@PathVariable("postId") Long postId){
        // 存在绑定则不允许删除
        SysUser user = new SysUser();
        user.setPostId(postId);
        Example<SysUser> example = Example.of(user);
        List<SysUser> users = userService.getExample(example);
        if (users.size() > 0){
            return ResponseDTO.wrap(HttpStatus.FORBIDDEN);
        }
        postService.deleteOne(postId);
        return ResponseDTO.succMsg("删除成功");
    }

    @PutMapping("posts")
    @ApiOperation(value = "岗位修改",notes = "")
    public ResponseDTO<Object> PostUpdate(@RequestBody @Validated PostUpdateParams params){
        Optional<SysPost> postOp = postService.getOne(params.getId());
        if (!postOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        SysPost post = postOp.get();
        BeanUtil.copyProperties(params,post,CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
        return ResponseDTO.succData(postService.updateOne(post));
    }

    @GetMapping("posts/{postId}")
    @ApiOperation(value = "岗位查询",notes = "")
    public ResponseDTO<Object> PostQuery(@PathVariable("postId") Long postId){
        Optional<SysPost> postOp = postService.getOne(postId);
        if (!postOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        SysPost post = postOp.get();
        return ResponseDTO.succData(post);
    }

    @GetMapping("posts")
    @ApiOperation(value = "岗位列表",notes = "")
    public ResponseDTO<Object> PostListQuery(PostQueryParams params){
        Sort sort;
        if(params.getOrder().equalsIgnoreCase("DESC")){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);
        /*
        构建 Example
         */
        SysPost post = new SysPost();
        BeanUtil.copyProperties(params,post,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<SysPost> example = Example.of(post);
        return ResponseDTO.succData(postService.getAll(pageable,example));
    }
}
